package com.sunmnet.j2ee.action.powerdb.designer;

import com.sunmnet.j2ee.action.BaseNewAction;
import com.sunmnet.j2ee.bean.pub.action.JsonResult;
import com.sunmnet.j2ee.core.pub.QueryFilter;
import com.sunmnet.j2ee.core.pub.annotation.MethodName;
import com.sunmnet.j2ee.entity.powerdb.designer.Project;
import com.sunmnet.j2ee.service.BaseService;
import com.sunmnet.j2ee.service.powerdb.designer.ProjectService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@Controller("/powerdb/designer/project")
@RequestMapping("/powerdb/designer/project")
public class ProjectAction extends BaseNewAction<Project, Long> {

	/**
	 * log4j2日志 *
	 */
	private static final Logger LOG = LogManager.getLogger(ProjectAction.class.getName());

	@Resource(name = "/powerdb/designer/projectService")
	@Override
	public void setService(BaseService<Project, Long> service) {
		super.service = service;
	}

	/**
	 * 批量查询
	 *
	 * @param request
	 * @return JsonResult
	 */
	@MethodName(name = "批量查询")
	@RequestMapping(value = "/", method = RequestMethod.PATCH)
	@ResponseBody
	public JsonResult find(HttpServletRequest request) {
		QueryFilter filter = new QueryFilter(request);
		return super.find(filter);
	}

	/**
	 * 新增
	 *
	 * @param obj
	 * @return JsonResult
	 */
	@MethodName(name = "新增")
	@RequestMapping(value = "/", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult add(Project obj) {
		if (((ProjectService) service).checkRepeat(obj) == 1) {
			JsonResult jsonResult = new JsonResult(false);
			jsonResult.setObj(obj);
			jsonResult.setMsg("项目【" + obj.getName() + "】已经存在，请重新命名！");
			return jsonResult;
		}
		return super.add(obj);
	}

	/**
	 * 修改
	 *
	 * @param obj
	 * @return JsonResult
	 */
	@MethodName(name = "修改")
	@RequestMapping(value = "/", method = RequestMethod.PUT)
	@ResponseBody
	public JsonResult update(Project obj) {
		if (((ProjectService) service).checkRepeat(obj) == 1) {
			JsonResult jsonResult = new JsonResult(false);
			jsonResult.setObj(obj);
			jsonResult.setMsg("项目【" + obj.getName() + "】已经存在，请重新命名！");
			return jsonResult;
		}
		return super.update(obj);
	}

	/**
	 * 删除
	 *
	 * @param id
	 * @return JsonResult
	 */
	@MethodName(name = "删除")
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public JsonResult delete(@PathVariable("id") long id) {
		return super.delete(id);
	}

	/**
	 * 批量删除
	 *
	 * @param ids 一个或多个id "1,2,3"
	 * @return JsonResult
	 */
	@MethodName(name = "批量删除")
	@RequestMapping(value = "/batch", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult batchDelete(@RequestParam(value = "ids", required = true) String ids) {
		return super.delete(ids);
	}

	/**
	 * 保存项目和账户关系
	 *
	 * @param projectId
	 * @param accountIds
	 * @param right      权限 true 读写 false只读
	 * @return JsonResult
	 */
	@MethodName(name = "保存项目与账户关系")
	@RequestMapping(value = "/savaRel", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult saveRel(@RequestParam(value = "id", required = true) Long projectId,
	                          @RequestParam(value = "ids", required = true) String accountIds,
	                          @RequestParam(value = "right", required = true) int right) {

		JsonResult json = new JsonResult(true);
		json.setMsg("保存项目与账户关系");
		try {
			((ProjectService) service).deleteProjectRelAccount(projectId, accountIds);
			json.setObj(((ProjectService) service).saveProjectRelAccount(projectId, accountIds, right));
		} catch (Exception e) {
			setErrMsg("保存项目与账户关系失败", LOG, json, e);
		}
		return json;
	}

	/**
	 * 删除项目和账户的关系
	 *
	 * @param projectId
	 * @param accountIds
	 * @return JsonResult
	 */
	@MethodName(name = "删除项目和账户关系")
	@RequestMapping(value = "/delRel", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult delRel(@RequestParam(value = "id", required = true) Long projectId,
	                         @RequestParam(value = "ids", required = true) String accountIds) {

		JsonResult json = new JsonResult(true);
		json.setMsg("删除项目和账户关系成功");
		try {
			json.setObj(((ProjectService) service).deleteProjectRelAccount(projectId, accountIds));
		} catch (Exception e) {
			setErrMsg("删除项目和账户关系失败", LOG, json, e);
		}
		return json;
	}

	/**
	 * 查询项目的所有用户
	 *
	 * @param projectId
	 * @return JsonResult
	 */
	@MethodName(name = "查询项目的所有用户")
	@RequestMapping(value = "/getAccounts", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult getAccounts(@RequestParam(value = "id", required = true) Long projectId) {

		JsonResult json = new JsonResult(true);
		json.setMsg("查询项目的所有用户成功");
		try {
			json.setObj(((ProjectService) service).getAccounts(projectId));
		} catch (Exception e) {
			setErrMsg("查询项目和账户关系失败", LOG, json, e);
		}
		return json;
	}

	/**
	 * 查询项目信息
	 *
	 * @param request
	 * @return JsonResult
	 */
	@MethodName(name = "查询项目信息")
	@RequestMapping(value = "/findProjectInfo", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult findProjectInfo(HttpServletRequest request) {

		JsonResult json = new JsonResult(true);
		QueryFilter filter = new QueryFilter(request);
		try {
			json.setObj(((ProjectService) service).getProjectInfo(filter));
			json.setMsg("查询项目信息成功");
		} catch (Exception e) {
			setErrMsg("查询项目信息失败", LOG, json, e);
		}
		return json;
	}

	/**
	 * 查询项目关联负责人名称
	 *
	 * @param projectId
	 * @return JsonResult
	 */
	@MethodName(name = "查询项目关联负责人名称")
	@RequestMapping(value = "/getProject/{id}", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult getProject(@PathVariable("id") long projectId) {

		JsonResult json = new JsonResult(true);
		try {
			json.setObj(((ProjectService)service).getProjectById(projectId));
			json.setMsg("查询项目成功");
		} catch (Exception e) {
			setErrMsg("查询项目失败", LOG, json, e);
		}
		return json;
	}


}
